package com.digitalart.site.section {
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;	

	import com.digitalart.site.assets.sectionContainerAnimation_FC;
	import flash.display.Sprite;

	/**
	 * @author johan.chaves
	 */
	public class SectionContainer extends Sprite {
		
		public static const ON_ANIMATION_IN_COMPLETE : String = "animationinComplete";
		public static const ON_ANIMATION_OUT_COMPLETE : String = "animationoutComplete";
		public static const ON_CONTAINER_OUT : String = "oncontainerout";
		
		public static const LOADER_POS_X : Number = 400;
		public static const LOADER_POS_Y : Number = 220;
		
		private var _animation : sectionContainerAnimation_FC;
		
		private var _currentSection : Section;
		
		private var _isInAnimation : Boolean;
		private var _isShowed : Boolean;
		private var _loader : GreenLoader;
		
		public function SectionContainer() {
			
			_animation = new sectionContainerAnimation_FC();
			this.addChild(_animation); 
			_isInAnimation = false;
			isShowed = false;
			addContainerEvents();
		}
		
		private function addContainerEvents() : void {
			
			_animation.addEventListener(ON_ANIMATION_IN_COMPLETE, onAnimationInComplete);
			_animation.addEventListener(ON_ANIMATION_OUT_COMPLETE, onAnimationOutComplete);
			addCloseEvents();
		}
		
		private function onAnimationOutComplete(event : Event) : void {
			
			_isInAnimation = false;
			isShowed = false;
			dispatchEvent(new Event(ON_CONTAINER_OUT));
		}

		private function onAnimationInComplete(event : Event) : void {	
					
			_isInAnimation = false;
			isShowed = true;
			dispatchEvent(new Event(ON_ANIMATION_IN_COMPLETE));
		}
		
		public function addCloseEvents() : void {
			
			_animation.home_mc.addEventListener(MouseEvent.CLICK, closeBtnClicked);
			_animation.home_mc.buttonMode = true;
		}
		
		public function removeCloseEvents() : void {
			
			_animation.home_mc.removeEventListener(MouseEvent.CLICK, closeBtnClicked);
		}
		
		private function closeBtnClicked(event : MouseEvent) : void {
			_animation.gotoAndPlay("out");
			removeSection();			
		}
		
		public function removeSection() : void {	
			
			if(_currentSection != null) {
				this.removeChild(_currentSection);
				_currentSection = null;
			}			
		}

		public function startInAnimation() : void {
			
			if(!isShowed) {										
				_animation.gotoAndPlay("in");
			}else{
				dispatchEvent(new Event(ON_ANIMATION_IN_COMPLETE));
			}
		}
		
		public function addSection(section : Section) : void {
			_currentSection = section;
			this.addChild(_currentSection);			
		}
		
		public function addLoader() : void {
	
			_loader = new GreenLoader();
			_loader.x = LOADER_POS_X;
			_loader.y = LOADER_POS_Y;
			this.addChild(_loader);
		}
		
		public function removeLoader() : void {
			
			this.removeChild(_loader);
			_loader = null;
		}
		
		public function get isShowed() : Boolean {
			return _isShowed;
		}
		
		public function set isShowed(isShowed : Boolean) : void {
			_isShowed = isShowed;
		}
	}
}
